﻿using Orchard.ContentManagement.MetaData;
using Orchard.Data.Migration;
using Orchard.Environment.Configuration;

namespace Coevery.OptionSet {
    public class Migrations : DataMigrationImpl {

        private readonly ShellSettings _shellSettings;

        public Migrations(ShellSettings shellSettings)
        {
            _shellSettings = shellSettings;
        }

        private string DataTablePrefix()
        {
            if (string.IsNullOrEmpty(_shellSettings.DataTablePrefix)) return string.Empty;
            return _shellSettings.DataTablePrefix + "_";
        }

        public int Create() {
            SchemaBuilder.CreateTable("OptionSetPartRecord", table => table
                .ContentPartRecord()
                .Column<string>("TermTypeName", column => column.WithLength(255))
                .Column<bool>("IsInternal")
                );

            SchemaBuilder.CreateTable("OptionItemPartRecord", table => table
                .ContentPartRecord()
                .Column<int>("OptionSetId")
                .Column<int>("Weight")
                .Column<bool>("Selectable")
                );

            SchemaBuilder.CreateTable("OptionItemContentItem", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<string>("Field", column => column.WithLength(250))
                .Column<int>("OptionItemRecord_id")
                .Column<int>("OptionItemContainerPartRecord_id")
                );

            SchemaBuilder.CreateTable("OptionItemContainerPartRecord", table => table
                .ContentPartRecord()
                );

            ContentDefinitionManager.AlterTypeDefinition("OptionSet", cfg => cfg
                .WithPart("OptionSetPart")
                .WithPart("TitlePart")
                .WithPart("CommonPart")
                );

            ContentDefinitionManager.AlterTypeDefinition("OptionItem", cfg => cfg
                .WithPart("OptionItemPart")
                .WithPart("TitlePart")
                .WithPart("CommonPart")
                );

            return 3;
        }

        public int UpdateFrom3()
        {
            SchemaBuilder.AlterTable("OptionItemPartRecord", table => table
                .AddColumn<int>("Parent_Id", c => c.Nullable())
                );

            SchemaBuilder.AlterTable("OptionItemPartRecord", table => table
               .AddColumn<int>("Parent_OptionSetId", c => c.Nullable())
               );
            return 4;
        }

        public int UpdateFrom4(){

            SchemaBuilder.ExecuteSql(string.Format(@" UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 37, Parent_OptionSetId = 36 WHERE Id = 42
                                        
                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 39, Parent_OptionSetId = 36 WHERE Id = 43

                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 38, Parent_OptionSetId = 36 WHERE Id = 44

                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 39, Parent_OptionSetId = 36 WHERE Id = 45

                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 42, Parent_OptionSetId = 41 WHERE Id = 47

                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 45, Parent_OptionSetId = 41 WHERE Id = 48

                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 44, Parent_OptionSetId = 41 WHERE Id = 49

                                        UPDATE {0}Coevery_OptionSet_OptionItemPartRecord
                                        SET	Parent_Id = 43, Parent_OptionSetId = 41 WHERE Id = 50", DataTablePrefix()));

            return 4;
        }
    }
}